//
//  CarStore.m
//  EquiriesProject
//
//  Created by qianfeng on 15/10/29.
//  Copyright (c) 2015年 HXH. All rights reserved.
//

#import "CarStore.h"
#import "OilModel.h"

#define carUrl @"http://wz.qichecdn.com/ashx/getaddress.ashx?&cityid=%d&token=2&platform=2&version=3.2.0&qd=kqzs_cpa_test_w"

@interface CarStore ()<BMKMapViewDelegate,BMKLocationServiceDelegate>

@end

@implementation CarStore
{
    BMKMapView * _mapView;
    BMKAnnotationView * _annotionView;
    BMKLocationService * _locationDervice;
    NSMutableArray * _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray = [NSMutableArray array];
    
    [self getData];
    //[self makeMap];
}

-(void)getData
{
    int number = [_num intValue];
    
    if (number == 0) {
        number = 110100;
    }
    
    [Fundction getWithUrl:[NSString stringWithFormat:carUrl,number] success:^(id responseObject) {
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        
        NSDictionary * result = dic[@"result"];
        NSArray * items = result[@"items"];
        for (NSDictionary * item in items) {
            OilModel * model = [[OilModel alloc]initWithDict:item];
            [_dataArray addObject:model];
        }
        [self makeMap];
    } failure:^(id error) {
        NSLog(@"error = %@",error);
    }];
}


-(void)makeMap
{
    _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 64, Width, Height)];
    _mapView.delegate = self;
    _mapView.mapType = 0;
    //显示位置
    _mapView.showsUserLocation = YES;
    
    //跟着模式
    _mapView.userTrackingMode = BMKUserTrackingModeFollowWithHeading;
    [self.view addSubview:_mapView];
    
    for (int i=0; i<_dataArray.count; i++) {
        OilModel * model;
        if (_dataArray.count == 0) {
            model = [[OilModel alloc] init];
        }else{
            model = _dataArray[i];
        }
        BMKPointAnnotation * pointAnnotation = [[BMKPointAnnotation alloc]init];
        pointAnnotation.coordinate = CLLocationCoordinate2DMake([model.latbaidu floatValue],[model.lngbaidu floatValue]);
        pointAnnotation.title = model.name;
        [_mapView addAnnotation:pointAnnotation];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
